package com.vito.dcs.common.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class SplitTableUtil {

	private static final String COMMON_TABLE_NAME = "tbl_dcs_data_paver_construction";
	private static final Logger log = LoggerFactory.getLogger(SplitTableUtil.class);
	
	public static String getTableName(Date d){
		String year_month = processYearAndMonth(d);
		return COMMON_TABLE_NAME + "_" + year_month;
	}
	public static String getTableName(String prevName, Date d){
		String year_month = processYearAndMonth(d);
		return prevName + "_" + year_month;
	}
	public static String getTableName(String d){
		Calendar c = Calendar.getInstance();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		try {
			String year_month = processYearAndMonth(d, c, sdf);
			return COMMON_TABLE_NAME + "_" + year_month;
		} catch (Exception e) {
			log.error("解析日期失败", e);
		}
		return "";
	}
	public static String getTableName(String prevName, String d) throws ParseException{
		Calendar c = Calendar.getInstance();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		try {
			String year_month = processYearAndMonth(d, c, sdf);
			return prevName + "_" + year_month;
		} catch (Exception e) {
			log.error("解析日期失败", e);
		}
		return "";
		
	}
	private static String processYearAndMonth(String d, Calendar c,
			SimpleDateFormat sdf) throws ParseException {
		Date parse = sdf.parse(d);
		c.setTime(parse);
		int year = c.get(Calendar.YEAR);
		int month = c.get(Calendar.MONTH) + 1;
		String year_month = year + "" + (month < 10 ? "0" + month : month);
		return year_month;
	}
	private static String processYearAndMonth(Date d){
		Calendar c = Calendar.getInstance();
		c.setTime(d);
		int year = c.get(Calendar.YEAR);
		int month = c.get(Calendar.MONTH) + 1;
		String year_month = year + "" + (month < 10 ? "0" + month : month);
		return year_month;
	}
	public static void main(String[] args) {
		System.out.println(getTableName(new Date()));
	}
}
